package com.spirngboot.middleware.middleware_rabbitmq.server.service;

import com.spirngboot.middleware.middleware_rabbitmq.server.rabbitmq.callback.MessageConfirmCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public void makeOrderFanout(Long userId, Long productId, int number) {
        String exchangeName = "fanout-order-exchange";
        String routingKey = "";
        //1.模拟用户下单
        String orderNumber = UUID.randomUUID().toString();
        // 2: 根据商品id productId 去查询商品的库存
        // int numstore = productSerivce.getProductNum(productId);
        // 3:判断库存是否充足
        // if(num >  numstore ){ return  "商品库存不足..."; }
        // 4: 下单逻辑
        // orderService.saveOrder(order);
        // 5: 下单成功要扣减库存
        // 6: 下单完成以后
        log.info("用户{}的订单{}完成,订购{}产品{}份", userId, orderNumber, productId, number);
        // 发送消息给RabbitMQ fanout
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderNumber);
    }

    @Override
    public void makeOrderDirect(Long userId, Long productId, int number) {
        String orderNumber = UUID.randomUUID().toString();
        String exchangeName = "direct-order-exchange";
        log.info("用户{}的订单{}完成,订购{}产品{}份", userId, orderNumber, productId, number);
        rabbitTemplate.convertAndSend(exchangeName, "wechat", orderNumber);
        log.info("给用户发送微信消息");
        rabbitTemplate.convertAndSend(exchangeName, "short_message", orderNumber);
        log.info("给用户发送短信消息");
    }

    @Override
    public void makeOrderTopic(Long userId, Long productId, int number) {
        String orderNumber = UUID.randomUUID().toString();
        String exchangeName = "topic-order-exchange";
        log.info("用户{}的订单{}完成,订购{}产品{}份", userId, orderNumber, productId, number);
        rabbitTemplate.convertAndSend(exchangeName, "com.short_message.order", orderNumber);
        log.info("给用户发送短信消息和微信消息");
    }

    @Override
    public void makeOrderTTL(Long userId, Long productId, int number) {
        String orderNumber = UUID.randomUUID().toString();
        String exchangeName = "ttl-exchange";
        log.info("用户{}的订单{}完成,订购{}产品{}份", userId, orderNumber, productId, number);
        rabbitTemplate.convertAndSend(exchangeName, "", orderNumber);
        log.info("给TTL队列发送消息");
    }

    @Override
    public void makeOrderTTLMessage(Long userId, Long productId, int number) {
        String orderNumber = UUID.randomUUID().toString();
        String exchangeName = "ttl-exchange";
        String routingKey = "ttl_message";
        log.info("用户{}的订单{}完成,订购{}产品{}份", userId, orderNumber, productId, number);

        // 给消息设置过期时间
        MessagePostProcessor messagePostProcessor = message -> {
            message.getMessageProperties().setExpiration("5000");
            message.getMessageProperties().setContentEncoding("UTF-8");
            return message;
        };

        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderNumber, messagePostProcessor);
        log.info("给ttl.message.direct.queue队列发送TTL消息");
    }

    @Override
        public void makeOrderTTLWithDeadQueue(Long userId, Long productId, int number) {
        String orderNumber = UUID.randomUUID().toString();
        String exchangeName = "orderTTL-exchange";
        String routingKey = "";
        log.info("用户{}的订单{}完成,订购{}产品{}份", userId, orderNumber, productId, number);

        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderNumber);
        log.info("给带有死信队列的TTL队列发送订单消息");
    }

    @Override
    public void makeOrderTopicWithCallBack(Long userId, Long productId, int number) {
        String orderNumber = UUID.randomUUID().toString();
        String exchangeName = "topic-order-exchange";
        log.info("用户{}的订单{}完成,订购{}产品{}份", userId, orderNumber, productId, number);
        //添加消息确认机制
        rabbitTemplate.setConfirmCallback(new MessageConfirmCallback());
        log.info("给{}添加消息确认机制",log.getName());
        rabbitTemplate.convertAndSend(exchangeName, "com.short_message.order", orderNumber);
        log.info("给用户发送短信消息和微信消息");
    }
}
